package mylibrary.concurrent;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class TestThreadGetData {
	
	private  static  int count;
	
	static{
		try {
			//Class.forName("net.sourceforge.jtds.jdbc.Driver");
			
			File file = new File("D:\\Study\\jtds-1.2.5.jar");
			//File file = new File("D:\\Tomcat_6.0.32\\lib\\mysql-connector-java-5.0.8-bin.jar");
			URL[] url = new URL[]{file.toURI().toURL()};;
			ClassLoader loader = new URLClassLoader(url);
			//Driver d = (Driver) Class.forName("com.mysql.jdbc.Driver", true, loader).newInstance();
			Driver d = (Driver) Class.forName("net.sourceforge.jtds.jdbc.Driver", true, loader).newInstance();
			DriverManager.registerDriver(new DriverShim(d));
			
			//ClassLoader.getSystemClassLoader().loadClass("com.mysql.jdbc.Driver").newInstance();   //load driver by system classLoader
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws ClassNotFoundException {
		
		final TestThreadGetData test = new TestThreadGetData();
		
		ExecutorService exec = Executors.newFixedThreadPool(5);
		
		List list = new ArrayList();
		for(int i=0; i<5; i++) {
			
			if (i==0) {
				Runnable run = new Runnable() {
					public void run() {
						final String sql = "select sessionId, login_Name from tbl_user_access_info"; 
						Connection conn = test.getConn();;
						test.getData_Write(conn, sql, "D:\\test0.txt");
						
					}
				};
				FutureTask task = new FutureTask(run, String.class);
				list.add(task);
			} else if (i==1) {
				Runnable run = new Runnable() {
					public void run() {
						final String sql = "select id, file_name from tbl_file_backup"; 
						Connection conn = test.getConn();;
						test.getData_Write(conn, sql, "D:\\test1.txt");
						
					}
				};
				FutureTask task = new FutureTask(run, String.class);
				list.add(task);
			}
		}
		for(int i=0; i<list.size(); i++) {
			exec.submit((FutureTask) list.get(i));
		}
		exec.shutdown();
		
		//System.out.println("Count = " + count);
	}
	
	public Connection getConn() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.205.83:1403", "ipdev", "ipdev");
			//conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/qiyexuexi", "root", "mysql");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public  String getData_Write(Connection conn, String sql, String filePath) {
		
		String result = "";
		Statement state = null;
		try {
			state = conn.prepareStatement(sql);
			ResultSet rs = state.executeQuery(sql);
			
			while (rs.next()) {
				String s1 = rs.getString(1);
				String s2 = rs.getString(2);
				result = s1 + " " + s2 + " at " + new Timestamp(System.currentTimeMillis());
				write2Text(filePath, result);
				synchronized(TestThreadGetData.class){
					count ++;
					if(result.contains("visitor") || result.contains("gaoshuaidage")) {
						System.out.println("sql 0 print: Count = " + count);
					} else {
						System.out.println("sql 1 print: Count = " + count);
					}
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				state.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return result;
	}
	
	public void write2Text(String filePath, String content) {
		
		FileWriter fw = null;
		BufferedWriter bw = null;
		try {
			File file = new File(filePath);
			boolean create = false;
			if (!file.exists()) {
				file.createNewFile();
				create = true;
			} 
			fw = new FileWriter(file, true);
			bw = new BufferedWriter(fw);
			if(!create) {
				bw.newLine();	
			}
			bw.write(content);		
			
			bw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				//fw.close();
				//bw.close();	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
}
